package com.zrrd.common.exception;

import com.zrrd.model.common.dtos.ResponseResult;
import com.zrrd.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author WanJl
 * @version 1.0
 * @title ExceptionCatch
 * @description 处理系统中各种异常的类
 * @create 2023/9/13 9:31
 */
@Slf4j
@ControllerAdvice   //Controller的增强，统一处理所有RequestMapping的方法的异常。
public class ExceptionCatch {
    //处理不可控异常的方法
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exception(Exception e){
        e.printStackTrace();
        log.error("catch exception:{}",e.getMessage());
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }


    //处理可控异常的方法 自定义异常处理方案
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ResponseResult exception(CustomException e){
        log.error("catch exception:{}",e);
        return ResponseResult.errorResult(e.getAppHttpCodeEnum());
    }
}
